﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MW.Domain.DC;
using MW.Persistence.MapperDao;
using IBatisNet.Common.Pagination;
using IBatisNet.Common.Utilities;
using IBatisNet.DataAccess.Interfaces;

namespace MW.Persistence.MapperDao.DC
{
    public class MetaSqlMapDao : BaseSqlMapDao, MW.Persistence.Interface.DC.IMetaDao
    {
        public Meta GetMeta(string metaId)
        {
            return (ExecuteQueryForObject("GetMetaByDCUniqueID", metaId) as Meta);
        }

        public IPaginatedList SearchMetaList(string keywords)
        {
            object parameterObject = new MetaSearch(keywords);
            return ExecuteQueryForPaginatedList("SearchMetaList", parameterObject, PAGE_SIZE);
        }

        public IPaginatedList SearchMetaDCList(string keywords)
        {
            object parameterObject = new MetaSearch(keywords);
            return ExecuteQueryForPaginatedList("SearchMetaDCList", parameterObject, PAGE_SIZE);
        }

        public IPaginatedList SearchMetaDCListColumn(string column, string keywords)
        {
            string strMethod = "SearchMetaDCListTitle";
            MetaSearch parameterObject = new MetaSearch(keywords);

            switch (column)
            {
                case "title":
                    strMethod = "SearchMetaDCListTitle";
                    break;
                case "creator":
                    strMethod = "SearchMetaDCListCreator";
                    break;
                case "publisher":
                    strMethod = "SearchMetaDCListPublisher";
                    break;
                case "subject":
                    strMethod = "SearchMetaDCListSubject";
                    break;
                case "description":
                    strMethod = "SearchMetaDCListDescription";
                    break;
            }
            return ExecuteQueryForPaginatedList(strMethod, "%" + keywords + "%", PAGE_SIZE);
        }

        public void InsertMeta(Meta meta)
        {
            ExecuteDelete("DeleteRecLink", meta);
            ExecuteDelete("DeleteMetaDC", meta);
            ExecuteDelete("DeleteMeta", meta);

            ExecuteInsert("InsertMeta", meta);
            foreach (MetaDC mItem in meta.MetaDCs)
            {
                mItem.Meta = meta;
                ExecuteInsert("InsertMetaDC", mItem);
            }
            foreach (RecLink rItem in meta.RecLinks)
            {
                rItem.Meta = meta;
                ExecuteInsert("InsertRecLink", rItem);
            }
        }

        public MetaDC GetMetaDCByUniqueID(string uniqueID)
        {
            return (ExecuteQueryForObject("GetMetaDCByUniqueID", uniqueID) as MetaDC);
        }

        public IPaginatedList GetRelatedMeta(MetaDC metaDC)
        {
            MetaSearch parameterObject = new MetaSearch(metaDC.Subject.Replace("|", " "));
            parameterObject.UniquedID = metaDC.Meta.UniqueID;
            return ExecuteQueryForPaginatedList("GetRelatedMeta", parameterObject, 5);
        }

        public class MetaSearch
        {
            private IList keywordList = new ArrayList();

            public MetaSearch(String keywords)
            {
                StringTokenizer splitter = new StringTokenizer(keywords, " ", false);
                string token = null;

                IEnumerator enumerator = splitter.GetEnumerator();

                while (enumerator.MoveNext())
                {
                    token = (string)enumerator.Current;
                    keywordList.Add("%" + token + "%");
                }
            }

            public IList KeywordList
            {
                get
                {
                    return keywordList;
                }
            }

            private string uniquedID;

            public string UniquedID
            {
                get { return uniquedID; }
                set { uniquedID = value; }
            }
        }
    }
}
